package com.xinchengxd.order.controller;

import com.xinchengxd.commons.ResponseResult;
import com.xinchengxd.commons.entity.dto.OrderDTO;
import com.xinchengxd.commons.entity.order.Order;
import com.xinchengxd.order.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 姚新成
 * @Date: 2021/11/8:21:51
 * @Description: 订单生成
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /*下单*/
    @RequestMapping("/place")
    public ResponseResult<String> place(@RequestBody OrderDTO orderDTO ,@RequestHeader("userID") String userIdTK) {

        /*从请求头取出userId 放入orderDTO中*/
        System.out.println("拿到的请求头ID是："+userIdTK);
        Integer userId = null;
        try {
            userId = Integer.valueOf(userIdTK);
        } catch (NumberFormatException e) {
           return new ResponseResult(501, "失败", "系统繁忙请稍后");
        }
        orderDTO.setUserId(userId);
        Order orderRes = orderService.place(orderDTO);

        return new ResponseResult(200, "ok", "订单号："+orderRes.getOrderNo());
    }

    /*判断订单是否支付*/
    @RequestMapping("/isPay")
    public Boolean isPay(String orderNo){
        return orderService.isPay(orderNo);
    }
}
